package com.vercon.commonmirror.base;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.autosize.AutoSize;

/**
 * Created by csh on 2021/1/25
 */
public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    private final Activity context;
    private final LayoutInflater inflater;
    public List<T> mList;
    private final int layoutId;
    protected OnItemClickListener<T> onItemClickListener;//单击事件
    protected OnItemLongClickListener<T> onItemLongClickListener;//长按单击事件
    private final boolean clickFlag = true;//单击事件和长单击事件的屏蔽标识

    public BaseRecyclerViewAdapter(Activity context,  int layoutId) {
        this.context = context;
        this.mList = new ArrayList<>();
        this.layoutId = layoutId;
        this.inflater = LayoutInflater.from(context);
    }

    public void setDataLists(List<T> lists) {
        this.mList = lists;
        notifyDataSetChanged();
    }

    public void notifyDataList(List<T> list) {
        this.mList = list;
    }

    public void addDataLists(List<T> lists) {
        mList.addAll(lists);
        notifyDataSetChanged();
    }

    public List<T> getData(){
        return mList;
    }

    public Activity getContext(){
        return context;
    }

    @NotNull
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        AutoSize.autoConvertDensityOfGlobal(context);
        return new BaseViewHolder(inflater.inflate(layoutId, parent, false));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        bindData(context, holder, mList.get(position), position);
    }


    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    protected abstract void bindData(Activity context, BaseViewHolder holder, T data, int position);

    public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener<T> onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemClickListener<T> {
        void onItemClickListener(View v, T data, int position);
    }

    public interface OnItemLongClickListener<T> {
        void onItemLongClickListener(View v, T data, int position);
    }
} 